package com.tc.service;

import com.tc.dao.ThingsDao;
import com.tc.dto.Envelope;
import com.tc.entity.ThingsEntity;
import com.tc.util.DateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.List;

public class ThingsService {
    private UserService userService;
    private ThingsDao thingsDao;
    public ThingsService(){
        this.userService=new UserService();
        this.thingsDao=new ThingsDao();
    }

    /**根据用户id获取事件列表
     *
     * @param envelope 信封
     * @return
     */
    public Envelope getAllThings(Envelope envelope){
        JSONObject infor= JSONObject.fromObject(envelope.getObj());
        Envelope env=userService.validUser(infor.getString("id"),infor.getString("password"));

        if(env.getCode()!=0)
            return env;
        try {
            List<ThingsEntity> list=thingsDao.getAllThings(infor.getString("id"));
            JSONArray jsonArray=new JSONArray();
            for(ThingsEntity thingsEntity:list){
                jsonArray.add(thingsEntity.toString());
            }
            return new Envelope(jsonArray);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelope.dbError;
        }
    }

    /**添加新的事件
     *
     * @param envelope
     * @return
     */
    public Envelope addNewThings(Envelope envelope){
        JSONObject infor=JSONObject.fromObject(envelope.getObj());  //获取携带的JSON
        Envelope env=userService.validUser(infor.getString("id"),infor.getString("password"));  //验证身份
        if(env.getCode()!=0)    //如果验证失败
            return env;
        JSONArray resArray=new JSONArray();//用于回复
        JSONArray things= JSONArray.fromObject(infor.get("things"));    //传输过来的是一个数组
        for(Object thing:things){
            JSONObject res=new JSONObject();//每一条回复
            JSONObject json=JSONObject.fromObject(thing);  //拿到每一个json
            ThingsEntity thingsEntity=new ThingsEntity();
            thingsEntity.setStatus(0);
            thingsEntity.setUserId(infor.getString("id"));
            thingsEntity.setName(json.getString("name"));
            thingsEntity.setAddTime(DateUtil.str2Date(json.getString("addTime")));
            thingsEntity.setEndTime(DateUtil.str2Date(json.getString("endTime")));
            thingsEntity.setMemo(json.getString("memo"));
            res.put("localId",json.getInt("localId"));
            try {
                int dbId=thingsDao.save(thingsEntity);
                res.put("dbId",dbId);
            } catch (Exception e) {
                e.printStackTrace();
                return Envelope.dbError;
            }
            resArray.add(res);
        }
        return new Envelope(resArray);
    }

    /**删除某个事件
     *
     * @param envelope
     * @return
     */
    public Envelope deleteThing(Envelope envelope){
        JSONObject infor= JSONObject.fromObject(envelope.getObj());
        Envelope env=userService.validUser(infor.getString("id"),infor.getString("password"));

        if(env.getCode()!=0)
            return env;
        JSONArray ids=JSONArray.fromObject(infor.get("thingsId"));
        ThingsEntity entity;
        for(Object object:ids) {

            int id=(Integer) (object);//新建id
            try {
                entity = thingsDao.get(id);
            } catch (Exception e) {
                e.printStackTrace();
                return Envelope.dbError;
            }
            entity.setStatus(1);
            try {
                thingsDao.update(entity);
            } catch (Exception e) {
                e.printStackTrace();
                return Envelope.dbError;
            }
        }
        return Envelope.success;
    }
    public static void main(String args[]){
        ThingsService thingsService=new ThingsService();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("id","zekdot");
        jsonObject.put("password","123456");
        JSONArray array=new JSONArray();
        /*JSONObject thing=new JSONObject();
        thing.put("localId",1);
        thing.put("name","测试");
        thing.put("addTime","2018-11-18 21:31:45");
        thing.put("endTime","2018-11-18 21:40:56");
        thing.put("memo","这条用于测试");
        array.add(thing);
        */
        //jsonObject.put("thingId",1);
        array.add(2);
        array.add(4);
        jsonObject.put("thingsId",array);
        System.out.println(new Envelope(jsonObject));
        System.out.println(thingsService.getAllThings(new Envelope(jsonObject)));
        //System.out.println(thingsService.deleteThing(new Envelope(jsonObject)));
        //System.out.println(thingsService.addNewThings(new Envelope(jsonObject)));
    }

}
